//
//  FeedbackVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/25.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//   建议反馈

#import "FeedbackVC.h"
#import "StringTool.h"
#import "MBProgressHUD+MJ.h"
#import "SettingDataManage.h"
#import "Connect.h"
#import <CoreText/CoreText.h>
#import "NSString+category.h"
#import "OpinionTypeView.h"
#import "config.h"


@interface FeedbackVC ()<OpinionTypeViewDelegate,UITextViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (nonatomic,strong)SettingDataManage *dataManage;
@property (weak, nonatomic) IBOutlet UITextField *phonetf;

@property (nonatomic,strong)OpinionTypeView *typeView;
@property (nonatomic,strong)UIButton *seletedBtn;//保存意见反馈中选择的按钮
@property (nonatomic,assign)NSInteger falg;//反馈页面出现的次数
@property (weak, nonatomic) IBOutlet UILabel *typeLab;
@property (nonatomic,strong)UIButton *seletedTypeBtn;
@property (weak, nonatomic) IBOutlet UIButton *phoneBtn;

@property (nonatomic,copy)NSString *emailStr;
@property (nonatomic,copy)NSString *phoneStr;

@end

@implementation FeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    

}

-(void)initView{
    [super initView];
    [self OpinionTypeViewBtnOnClicked:self.typeView.fristBtn];
    [self typeBtnOnClick:self.phoneBtn];
    
    [self.titleView.doneBtn setImage:nil forState:UIControlStateNormal];
    [self.titleView setMianTitle:@"用户反馈"];
    [self.titleView.doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.titleView.doneBtn setAttributedTitle:[NSString makeLineStyle:@"提交" color:[UIColor whiteColor] fontSize:14] forState:UIControlStateNormal];
    [self.titleView.doneBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.dataManage = [[SettingDataManage alloc]init];
    
    self.typeView = [OpinionTypeView createView];
    self.typeView.delegate = self;

}

- (void)textViewDidBeginEditing:(UITextView *)textView;

{
    textView.text = @"";
}
#pragma mark 提交
- (void)btnOnClick:(id)sender {
    
    if (self.typeLab.text.length == 0 ){
        
        [MBProgressHUD showError:@"请选择反馈类型"];
        return;
    }else if ([StringTool isBlankString:self.textview.text]||[self.textview.text isEqualToString:@"请输入产品意见，我们将不断优化体验"]) {
        [MBProgressHUD showError:@"请输入您的宝贵意见"];
        return;
    }
    
    if (![StringTool isBlankString:self.phonetf.text]) {
        
        if (self.phoneBtn.selected) {
            
            self.emailStr = @"";
            
            if (![self validateMobile:self.phonetf.text]) {
                
                [MBProgressHUD showError:@"请输入正确的手机号"];
                return;
            }
            self.phoneStr = self.phonetf.text;
        }else
        {
            if (![NSString isValidateEmail:self.phonetf.text]) {
                
                 [MBProgressHUD showError:@"请输入正确的电子邮箱"];
                return;
            }
            self.emailStr = self.phonetf.text;
            self.phoneStr = @"";
            
        }
    }else{
        
        self.emailStr = @"";
        self.phoneStr = @"";
    }
    [self uPdata];
}

-(void)uPdata
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ResulteMod *mod = [bself.dataManage feedbacknew:self.textview.text mobile:self.phoneStr email:self.emailStr feedbackType:self.typeLab.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError){
                [MBProgressHUD showError:@"网络错误"];
            }else{
                [bself.navigationController popViewControllerAnimated:YES];
                [MBProgressHUD showSuccess:@"提交成功"];
            }
            
        });
    });
}

#pragma mark 反馈类型
- (IBAction)typeBtnOnClick {
    
    [self.view endEditing:YES];
    [self.typeView showInView:self.view];
}

#pragma mark 反馈类型界面的delegate
-(void)OpinionTypeViewBtnOnClicked:(UIButton *)btn{
    
    if (btn.tag == 100) {
        [self.typeView hide];
        self.typeLab.text = self.seletedBtn.currentTitle;
    
    }else{
        
        self.seletedBtn.selected = NO;
        btn.selected = YES;
        self.seletedBtn = btn;
    }
    
}

-(void)saveBtnOnClicked:(NSString *)title{
    
    self.typeLab.text = title;
}
- (IBAction)typeBtnOnClick:(UIButton *)sender {
    
    self.seletedTypeBtn.selected = NO;
    sender.selected = YES;
    self.seletedTypeBtn = sender;
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
//    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.view];
    CGRect dd =  textField.superview.frame;
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-250-tvbotY;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.view.frame;
        buf.origin.y = up ? mid : 0;
        [self.view setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.view.frame;
    buf.origin.y = 64;
    [self.view setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}

@end
